const SparkMD5 = require('spark-md5')
module.exports = (options, app) => {
    return async (ctx, next) => {
      const data = JSON.parse(JSON.stringify(ctx.request.method === 'GET' ? ctx.query : ctx.request.body))

      if (!data.openid) {
        ctx.body = {
            code: 50001,
            msg: '缺少参数 openid',
            data: {}
        }
        return
      }

      const userTable = options.userTable || 'watchdog_user'
      const getUser = await app.mysql.get(userTable, {
        openid: data.openid
      })
      
      if (!getUser) {
        ctx.body = {
            code: 50002,
            msg: '用户不存在',
            data: {}
        }
        return
      }

      ctx.currentUser = getUser
      
      await next()
    }
  }